<template>
    <ul>
        <li class="news" v-for="(item, index) in listData.data" 
        :key="index">
            <div class="left">
                <div class="title">{{ item.title }}</div>
                <div class="info">
                    <span>{{ item.source }}</span>
                    <span>{{ item.time }}</span>
                </div>
            </div>
            <div class="right">
                <img :src="item.img" alt="">
            </div>
        </li>
    </ul>
</template>
<script setup>
import { onBeforeMount, reactive } from 'vue'
import axios from 'axios'
// 定义发请求的方法
const request = axios.create({
    timeout: 2000 // 2秒超时时间
})
const listData = reactive({ data: [], message: '' })
// console.log(typeof request) // 方法
onBeforeMount(() => {
    // 发请求
    request({
        url: 'http://hmajax.itheima.net/api/news',
        method: 'get',
    }).then(result => {
        // console.log('请求成功')
        // console.log(result)
        // console.log(result.data)
        listData.data = result.data.data
        listData.message = result.data.message
    }).catch(error => {
        console.log(error)
    })
})

</script>
<style scoped>
* {
    margin: 0;
    padding: 0;
    list-style: none;
}

.news {
    display: flex;
    height: 120px;
    width: 600px;
    margin: 0 auto;
    padding: 20px 0;
    cursor: pointer;
}

.news .left {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding-right: 10px;
}

.news .left .title {
    font-size: 20px;
}

.news .left .info {
    color: #999999;
}

.news .left .info span {
    margin-right: 20px;
}

.news .right {
    width: 160px;
    height: 120px;
}

.news .right img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
</style>